﻿using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Hopscotch.Presentation.Common;

namespace Hopscotch.Presentation.Controls.Core
{
    /// <summary>
    /// Interaction logic for QuantityControl.xaml
    /// </summary>
    public partial class QuantityControl : UserControlBase
    {
        private const string QuantityPropertyName = "Quantity";

        public QuantityControl()
        {
            Validation.AddErrorHandler(this, OnBindingValidationError);

            InitializeComponent();
        }

        private void OnBindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            RaiseErrorsChanged(QuantityPropertyName);
        }

        public decimal Increment
        {
            get { return (decimal)this.GetValue(IncrementProperty); }
            set { this.SetValue(IncrementProperty, value); }
        }

        public decimal Quantity
        {
            get { return (decimal)this.GetValue(QuantityProperty); }
            set { this.SetValue(QuantityProperty, value); }
        }

        public static readonly DependencyProperty QuantityProperty
            = DependencyProperty.Register(QuantityPropertyName, typeof(decimal), typeof(QuantityControl), new PropertyMetadata(null));

        public static readonly DependencyProperty IncrementProperty
            = DependencyProperty.Register("Increment", typeof(decimal), typeof(QuantityControl), new PropertyMetadata(1.0M));


        public override IEnumerable GetErrors(string propertyName)
        {
            if (propertyName == QuantityPropertyName)
            {
                if (Validation.GetErrors(this).Count > 0)
                {
                    var list = Validation.GetErrors(this).Select(x => x.ErrorContent).ToList();
                    return list;
                }
            }

            return null;
        }

    }
}
